package com.wswy.wzcx.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import com.wswy.wzcx.R;

public class HintAlertDialog extends Dialog implements OnClickListener{

  public HintAlertDialog(@NonNull Context context) {
    super(context);
  }


  private String title;
  private String message;
  private String leftBtnText;
  private String rightBtnText;

  private View.OnClickListener leftOnClickListener;
  private View.OnClickListener rightOnClickListener;

  public HintAlertDialog setDialogTitle(String title){
    this.title = title;
    return this;
  }

  public HintAlertDialog setDialogTitle(@StringRes int titleResId){
    this.title = getContext().getString(titleResId);
    return this;
  }

  public HintAlertDialog setDialogMessage(String message){
    this.message = message;
    return this;
  }

  public HintAlertDialog setDialogMessage(@StringRes int messageResId){
    this.message = getContext().getString(messageResId);
    return this;
  }


  public HintAlertDialog setLeftText(String leftBtnText,View.OnClickListener clickListener){
    this.leftBtnText = leftBtnText;
    leftOnClickListener = clickListener;
    return this;
  }

  public HintAlertDialog setLeftText(@StringRes int leftBtnTextResId,View.OnClickListener clickListener){
    this.leftBtnText = getContext().getString(leftBtnTextResId);
    leftOnClickListener = clickListener;
    return this;
  }


  public HintAlertDialog setRightText(String rightBtnText,View.OnClickListener clickListener){
    this.rightBtnText = rightBtnText;
    rightOnClickListener = clickListener;
    return this;
  }

  public HintAlertDialog setRightText(@StringRes int rightBtnTextResId,View.OnClickListener clickListener){
    this.rightBtnText = getContext().getString(rightBtnTextResId);
    rightOnClickListener = clickListener;
    return this;
  }



  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    setContentView(R.layout.dialog_hint_alert);


    TextView tvTitle = findViewById(R.id.tv_title);
    TextView tvMessage = findViewById(R.id.tv_message);
    TextView btnLeft = findViewById(R.id.btn_left);
    TextView btnRight = findViewById(R.id.btn_right);

    if(title != null){
      tvTitle.setText(title);
    }
    tvMessage.setText(message);
    btnLeft.setText(leftBtnText);
    btnRight.setText(rightBtnText);

    btnLeft.setOnClickListener(this);
    btnRight.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    dismiss();
    int id = v.getId();
    if(id == R.id.btn_left){

      if(leftOnClickListener != null){
        leftOnClickListener.onClick(v);
      }
    }else if(id == R.id.btn_right){
      if(rightOnClickListener != null){
        rightOnClickListener.onClick(v);
      }
    }
  }
}
